<?php

namespace app\health\model;

use think\Model;

class HealthUserAccount extends Model
{
    // 表名
    protected $name = 'health_user_account';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;
    //收入
    public const CHANGE_TYPE_INCOME = 1;
    //支出
    public const CHANGE_TYPE_EXPEND = 2;
    //余额
    public const ACCOUNT_TYPE_BALANCE = 1;
    //虚拟币
    public const ACCOUNT_TYPE_VIRTUALLY = 2;
    // 签到
    public const FORM_TYPE_SIGNIN = 1;
    // 提现
    public const FORM_TYPE_WITHDRAW = 2;
    // 余额抵扣
    public const FORM_TYPE_DEDUCTION = 3;
    // 活动
    public const FORM_TYPE_ACTIVITY = 4;
    //网赚任务
    public const FORM_TYPE_USER_TASK = 5;
    //健康+每日任务
    public const FORM_TYPE_HEALTH_PLUS_EVERYDAY = 6;


    // 类型描述
    public const MAP_TYPE = [
        'change_type'  => [
            self::CHANGE_TYPE_INCOME => '收入',
            self::CHANGE_TYPE_EXPEND => '支出',
        ],
        'account_type' => [
            self::ACCOUNT_TYPE_BALANCE   => '余额',
            self::ACCOUNT_TYPE_VIRTUALLY => '健康币',
        ],
        'form_type'    => [
            self::FORM_TYPE_SIGNIN    => '签到',
            self::FORM_TYPE_WITHDRAW  => '提现',
            self::FORM_TYPE_DEDUCTION => '余额抵扣',
            self::FORM_TYPE_ACTIVITY  => '营销活动',
            self::FORM_TYPE_USER_TASK => '网赚任务',
        ]
    ];

    /**
     * 来源类型描述
     *
     * @param [type] $formType
     * @return array|string
     */
    public static function formTypeToDesc($formType = '')
    {
        $data = [
            self::FORM_TYPE_SIGNIN               => '每日签到',
            self::FORM_TYPE_WITHDRAW             => '提现申请',
            self::FORM_TYPE_DEDUCTION            => '余额抵扣',
            self::FORM_TYPE_ACTIVITY             => '营销活动',
            self::FORM_TYPE_USER_TASK            => '网赚任务',
            self::FORM_TYPE_HEALTH_PLUS_EVERYDAY => '健康+每日任务',
        ];
        if (!empty($formType)) {
            return $data[$formType] ?? '';
        }
        return $data;
    }


    /**
     * 增加账户收益记录
     * @param [type] $userId
     * @param [type] $formType
     * @param [type] $accountType
     * @param [type] $value
     * @param string $desc
     * @return static
     */
    public static function income($userId, $formType, $accountType, $value, string $desc = ''): HealthUserAccount
    {
        $desc = $desc ?: self::formTypeToDesc($formType);
        return self::create([
            'user_id'      => $userId,
            'form_type'    => $formType,
            'account_type' => $accountType,
            'change_type'  => self::CHANGE_TYPE_INCOME,
            'value'        => $value,
            'desc'         => $desc,
            'created_at'   => date('Y-m-d H:i:s')
        ]);
    }

    /**
     * 增加账户支出记录
     * @param [type] $userId
     * @param [type] $formType
     * @param [type] $accountType
     * @param [type] $value
     * @param string $desc
     * @return static
     */
    public static function expend($userId, $formType, $accountType, $value, $desc = '')
    {
        $desc = $desc ?: self::formTypeToDesc($formType);
        return self::create([
            'user_id'      => $userId,
            'form_type'    => $formType,
            'account_type' => $accountType,
            'change_type'  => self::CHANGE_TYPE_EXPEND,
            'value'        => $value,
            'desc'         => $desc,
            'created_at'   => date('Y-m-d H:i:s')
        ]);
    }
}
